﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FilStartUps_Mobile.Models;


namespace FilStartUps_Mobile.Controllers
{
    public class ForumController : Controller
    {
        //
        // GET: /Forum/
        FilStartUpsEntities db_connect = new FilStartUpsEntities();
 
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ForumDisplay()
        {
            var forumdisplay = new  ForumIndex();
           
            return View(forumdisplay);
        }
        [HttpGet]
        public ActionResult Thread(string forumidFK)
        {
            int forumid = int.Parse(forumidFK);
            var forumthread = (from f in db_connect.ForumThreads
                               let cCount =(from fm in db_connect.ForumMessages
                                       where fm.forumthreadidFK.Equals(f.forumthreadidPK)
                                            select fm).Count()
                               where f.forumidFK.Equals(forumid)
                               select new Forum_Thread
                               {
                                   datecreated=f.datecreated,
                                   forumthreadidPK=f.forumthreadidPK,
                                   threadtitle=f.threadtitle,
                                   views=f.views,
                                   totalpost=cCount
                               }).ToList();
                
                //db_connect.ForumThreads.Where(f => f.forumidFK.Equals(forumid)).ToList();
           
            if (forumthread.Count() > 0)
            {
                return View(forumthread);
            }
            else
            {
                return View();
            }
        }

        public ActionResult ShowMessage(int forumthreadid)
        {
            try
            {
                int forumthreadid_ = int.Parse(Request.QueryString["t"].Trim());
                var msg = db_connect.ForumMessages.Where(f => f.forumthreadidFK.Equals(forumthreadid_)).ToList();
                if (msg.Count() > 0)
                {
                    return View(msg);
                }
                else
                {
                    return View();
                }
            }
            catch
            {

            }
            return View();
        }

        [HttpGet]
        public ActionResult NewThread()
        {
            return View();
        }

        [HttpPost]
        public ActionResult NewThread(ForumThread forum, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                ForumMessage forummsg = new ForumMessage();
                ForumThread forumthread = new ForumThread();

                int threadid = int.Parse(Request.QueryString["t"].Trim());
                forumthread.threadtitle = forum.threadtitle;
                forumthread.datecreated = DateTime.Now;
                forumthread.disabled = false;
                forumthread.forumidFK = threadid;
                db_connect.ForumThreads.Add(forumthread);


                forummsg.dateposted = DateTime.Now;
                forummsg.disabled = false;
                forummsg.forumthreadidFK = forumthread.forumthreadidPK;
                forummsg.message = form["forummessage"].Trim();
                db_connect.ForumMessages.Add(forummsg);
            }
            return View();
        }

        [HttpGet]
        public ActionResult ThreadReply()
        {
            int threadid = int.Parse(Request.QueryString["t"].Trim());
            var post = db_connect.ForumThreads.Where(t => t.forumthreadidPK.Equals(threadid)).SingleOrDefault();
            ViewBag.Subject = post.threadtitle;
                
            var previouspost = (from fm in db_connect.ForumMessages
                                join m in db_connect.Members
                                    on fm.memberidFK equals m.memberidPK
                                where fm.forumthreadidFK.Equals(threadid)
                                && fm.disabled.Equals(false)
                                select new ForumMessageDisplay
                                {
                                     dateposted=fm.dateposted,
                                     email=m.emailaddress,
                                     message=fm.message
                                }).ToList() ;
            return View(previouspost);
        }

        [HttpPost]
        public ActionResult ThreadReply(ForumMessage forummsg)
        {
            return View();
        }
    }
}
